<template>
  <div class='container'>
    <el-card>
      <!-- alert 提示 -->
          <el-alert
        :title="`数据一共 ${totals} 条`"
        style="margin-bottom:15px"
        type="info"
        :closable="false"
        show-icon
      ></el-alert>
      <!-- table 表格 -->
        <el-table  :data="questionList" >
        <el-table-column  label="试题编号" prop="number" width="120px"></el-table-column>
        <el-table-column label="学科" prop="subject" width="120px"></el-table-column>
        <el-table-column label="目录" prop="catalog" width="120px"></el-table-column>
        <el-table-column label="题型" prop="questionType" :formatter="formatQuest" width="120px">

        </el-table-column>
        <el-table-column label="题干" width="280px">
          <template #default="{row}">
            <div v-html="row.question"></div>
          </template>
        </el-table-column>
        <el-table-column label="录入时间" width="180" prop="addDate" >
          <template #default="{row}" >
            {{row.addDate|parseTimeByString()}}
          </template>
        </el-table-column>
        <el-table-column prop="difficulty" :formatter="formatDiff" label="难度">

        </el-table-column>
        <el-table-column label="录入人" width="120px" prop="creator" ></el-table-column>
        <el-table-column label="审核状态" width="120px" >
          <template #default="{row}">
            {{row.chkState|state_chk}}
          </template>
        </el-table-column>
        <el-table-column label="审核意见" prop="chkRemarks" width="150px" ></el-table-column>
        <el-table-column label="审核人" prop="chkUser" width="120px"></el-table-column>
        <el-table-column label="发布状态" width="150px" >
          <template #default="{row}">
            {{row|state_pub}}
           </template>
        </el-table-column>
        <el-table-column label="操作" width="200px" fixed="right" align="center">
          <template #default="{row}">
            <el-button class="fs" type="text" @click="handlePreview(row)" >预览</el-button>
            <el-button class="fs" type="text"  @click="handleCheckDialog(row)" :disabled="row.chkState!==0">审核</el-button>
            <el-button class="fs" type="text" :disabled="row.publishState===1" @click="$router.push(`new?id=${row.id}`)">修改</el-button>
            <el-button class="fs" type="text" @click="handleTb(row)" >{{row.publishState===1?'下架':'上架'}}</el-button>
            <el-button class="fs" type="text" @click="handleDel(row)" :disabled="row.publishState===1" >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

 <!-- 分页 -->

    <div class="pagination" >
           <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[5, 10,20, 30]"
      :page-size="5"
      background
      layout=" prev, pager,next,sizes,jumper"
      :total="totals">
    </el-pagination>
    </div>
    </el-card>
       <!-- 预览dialog -->
    <QuestionPreview :data="data"  ></QuestionPreview>
    <!-- 审核dialog -->
    <el-dialog title="题目审核" :visible.sync="show" width="400px">
         <el-form size="small">
        <el-form-item>
          <el-radio :label="1" v-model="checkData.chkState">通过</el-radio>
          <el-radio :label="2" v-model="checkData.chkState">拒绝</el-radio>
        </el-form-item>
        <el-form-item>
          <el-input type="textarea" v-model="checkData.chkRemarks" placeholder="请输入审核意见"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
         <el-button @click="show=false">取消</el-button>
        <el-button @click="confirm()" type="primary">确认</el-button>
      </template>

    </el-dialog>
  </div>
</template>

<script>
import { difficulty, questionType } from '@/api/hmmm/constants'
import { choice as reqgetList, choiceCheck, choicePublish, remove as delQuestion } from '@/api/hmmm/questions'
import QuestionPreview from '../components/questions-preview.vue'
export default {
  components: {
    QuestionPreview
  },
  data () {
    return {
      checkData: {
        id: null,
        chkState: 1,
        chkRemarks: null
      },
      show: false,
      difficulty,
      questionType,
      totals: 0,
      page: 1,
      pagesize: 5,
      questionList: [],
      // 预览框
      data: {
        show: false,
        info: {}
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    async getList () {
      const { data } = await reqgetList({ page: this.page, pagesize: this.pagesize })
      this.totals = data.counts
      this.questionList = data.items
      console.log(data)
    },
    // 过滤筛选难度等
    formatDiff (row, clo, cell, index) {
      const obj = this.difficulty.find(item => item.value === +cell)
      return obj.label
    },
    // 题型
    formatQuest (row, col, cell) {
      const obj = this.questionType.find(item => item.value === +cell)
      return obj.label
    },
    handlePreview (info) {
      this.data.show = true
      this.data.info = info
    },
    // 分页
    handleCurrentChange (val) {
      console.log(val, 'w')
      this.page = val
      this.getList()
    },
    handleSizeChange (val) {
      console.log(val, 'q')
      this.pagesize = val
      this.page = 1
      this.getList()
    },
    // 审核框
    handleCheckDialog (row) {
      this.show = true
      this.checkData.id = row.id
    },
    // 提交审核
    async confirm () {
      if (!this.checkData.chkRemarks) return this.$message.warning('请输入审核意见')
      await choiceCheck(this.checkData)
      this.$message.success('操作成功')
      this.show = false
      this.getList()
    },
    // 上下架
    async handleTb (question) {
      await this.$confirm(`您确认${question.publishState === 1 ? '下架' : '上架'}这道题目吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      question.publishState = question.publishState === 1 ? 0 : 1
      await choicePublish(question)
      this.$message.success(`${question.publishState === 0 ? '下架' : '上架'}成功`)
      // question.publishState = !question.publishState
      this.getList()
    },
    // 删除
    async handleDel (row) {
      await this.$confirm('此操作将永久删除该题目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await delQuestion(row)
      this.$message.success('删除成功')
      this.getList()
    }
  },
  filters: {
    state_chk (val) {
      if (val === 0) return '待审核'
      if (val === 1) return '已审核'
      if (val === 2) return '已拒绝'
    },
    state_pub (row) {
      if (row.publishState === 1 && row.chkState === 1) {
        return '已发布'
      }
      if (row.publishState === 0 && row.chkState === 1) {
        return '已下架'
      }
      return '待发布'
    }
  }
}
</script>

<style scoped lang='scss'>
.fs{
  font-size: 12px;
}
.pagination{
  float: right;
}
</style>
